package com.chenfan.mcn.enums;

import com.chenfan.common.extension.dict.DictTranslator;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;

/**
 * @author tangwei
 * Date:     2021/1/20 13:57
 * Description: 收入合同类型
 */
@AllArgsConstructor
public enum IncomeContractTypeEnum implements DictTranslator<Integer, String> {
    /**
     * 收入合同类型
     */

    ANNNUAL_DISCOUNT_INCOME_CONSTRACT(0, "年度优惠-收入合同"),
    ANNUAL_DISCOUNT_FORMAL_CONSTRACT(1, "年度优惠-形式合同"),
    SINGLE_ORDERS_INCOME_CONSTRACT(2, "散单-收入合同"),
    SINGLE_ORDERS_FORMAL_CONSTRACT(3, "散单-形式合同"),
    FRAME_CONTRACT(4, "原框架合同"),
    LIVE_CONTRACT(5, "框架合同"),
    ;

    @Getter
    private Integer type;

    @Getter
    private String desc;

    public static IncomeContractTypeEnum getType(Integer type) {
        if(Objects.isNull(type)) {
            return null;
        }
        for (IncomeContractTypeEnum value : IncomeContractTypeEnum.values()) {
            if (value.type.equals(type)) {
                return value;
            }
        }
        return null;
    }

    public static String getContractTypeName(Integer type) {
        IncomeContractTypeEnum incomeContractTypeEnum = getType(type);
        return incomeContractTypeEnum != null ? incomeContractTypeEnum.getDesc() : null;
    }

    /**
     * 是否是'框架合同'（原‘直播合同’）
     *
     * @param type
     * @return
     */
    public static boolean isLiveContract(Integer type) {
        return IncomeContractTypeEnum.LIVE_CONTRACT.getType().equals(type);
    }

    /**
     * 是否是'原框架合同'（原‘框架合同’）
     *
     * @param type
     * @return
     */
    public static boolean isFrameContract(Integer type) {
        return IncomeContractTypeEnum.FRAME_CONTRACT.getType().equals(type);
    }


    /**
     * 是否是形式类合同，包括：年度优惠-形式合同、散单-形式合同
     *
     * @param type
     * @return
     */
    public static boolean isFormalContract(Integer type) {
        return IncomeContractTypeEnum.ANNUAL_DISCOUNT_FORMAL_CONSTRACT.getType().equals(type)
                || IncomeContractTypeEnum.SINGLE_ORDERS_FORMAL_CONSTRACT.getType().equals(type);
    }

    /**
     * 是否需要校验执行单金额
     *
     * @param type
     * @return
     */
    public static boolean needVerifyExcutionAmount(Integer type) {
        return !IncomeContractTypeEnum.LIVE_CONTRACT.getType().equals(type)
                && !IncomeContractTypeEnum.LIVE_CONTRACT.getType().equals(type);
    }

    /**
     * 是否存在KOC执行单【年度优惠-收入合同】、【散单-收入合同】、【框架合同】
     * @param type
     * @return
     */
    public static boolean existKocExcutionOrder(Integer type) {
        return IncomeContractTypeEnum.ANNNUAL_DISCOUNT_INCOME_CONSTRACT.getType().equals(type)
                || IncomeContractTypeEnum.SINGLE_ORDERS_INCOME_CONSTRACT.getType().equals(type)
                || IncomeContractTypeEnum.LIVE_CONTRACT.getType().equals(type);
    }

    /**
     * 仅散单-收入合同、年度优化-收入合同、框架合同、直播合同，四种类型的合同允许修改开票抬头，其他合同类型不允许
     * @param type
     * @return
     */
    public static boolean allowChangeInvoice(Integer type) {
        return IncomeContractTypeEnum.ANNNUAL_DISCOUNT_INCOME_CONSTRACT.getType().equals(type)
                || IncomeContractTypeEnum.SINGLE_ORDERS_INCOME_CONSTRACT.getType().equals(type)
                || IncomeContractTypeEnum.LIVE_CONTRACT.getType().equals(type)
                || IncomeContractTypeEnum.LIVE_CONTRACT.getType().equals(type);
    }


    /** 根据状态码任意匹配
     * @param code
     * @param items
     * @return
     */
    public static boolean anyMatch(Integer code, IncomeContractTypeEnum... items) {
        boolean match = false;
        if(code != null) {
            for(IncomeContractTypeEnum item : items) {
                if(item.getType() == code.intValue()) {
                    match = true;
                    break;
                }
            }
        }
        return match;
    }

    @Override
    public String translate(Integer type) {
        return Objects.nonNull(type) ? getContractTypeName(type.intValue()) : null;
    }
}
